import {IMenuConfig} from "@/common/menu";
import moment from "moment";

export const API_DATETIME_FMT = "YYYY-MM-DD HH:mm:ss";

export const formatData = (dataStr: string) => {
  return moment(dataStr).format(API_DATETIME_FMT);
}

export function getOpenKeys(currentMenus: IMenuConfig[]) {
  if (!currentMenus || currentMenus.length === 0) {
    return [];
  }
  const openKeys: any = []
  currentMenus.map((menu) => {
    if (menu.showChild) {
      openKeys.push(menu.path)
    }
  })
  return openKeys
}

export function getSelectedKeys(currentMenus: IMenuConfig[]) {
  if (!currentMenus || currentMenus.length === 0) {
    return [];
  }
  if (currentMenus.length <= 1) {
    return [currentMenus[0].path]
  } else if (currentMenus.length > 1 && currentMenus.length < 3) {
    return [currentMenus[1].path]
  } else {
    return [currentMenus[2].path]
  }
}

export function randomString(length = 8) {
  const str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let target = "";
  for (let i = 0; i < length; i++) {
    target += str.charAt(Math.round(Math.random() * str.length));
  }
  return target;
}

//自定义处理DatePicker组件中的时间处理
export function disabledDate(date: moment.Moment) {
  return date && date > moment().endOf("day"); //当天之后日期不可选
}

export function parseMomentArr(times: moment.Moment[], format: string) {
  const result: (string | undefined)[] = [];
  for (const t of times) {
    if (!t) {
      result.push(undefined);
      continue;
    }
    result.push(t.format(format));
  }
  return result;
}

export function antDSelectFilterOption(input: string, opt: any) {
  console.log('input:' + input + 'opt:' + opt)
  if (opt && opt.children)
    return (
      opt.children
        .toString()
        .toLowerCase()
        .indexOf(input.toString().toLowerCase()) > -1
    );
  return false;
}

export const TOKEN = "XB_token";
export const USER_INFO = "XB_user_info";

export const MENU = "MENU";
export const VISIBEL = "COMPONENTS_VISIBEL";

/**
 * 删除的一组本地数据
 * @param {Array} keys 删除的一组本地数据的键值
 */
export function clearLocalDatas(keys: any) {
  keys.forEach((key: string) => {
    rmKey(true, key);
    rmKey(false, key);
  });
}

export function getKey(isLocal: boolean, key: string) {
  return JSON.parse(getStorage(isLocal).getItem(key) || "null");
}

export function rmKey(isLocal: boolean, key: string) {
  getStorage(isLocal).removeItem(key);
}

export function getStorage(isLocal: boolean) {
  return isLocal ? window.localStorage : window.sessionStorage;
}

export function getToken() {
  return window.localStorage.getItem(TOKEN)
}

export function setUserInfo(info: any) {
  window.localStorage.setItem(TOKEN, info.token)
  window.localStorage.setItem(USER_INFO, JSON.stringify(info))
}

export function clearUserInfo() {
  window.localStorage.removeItem(USER_INFO)
}

export function getUserInfo() {
  const loginInfo = window.localStorage.getItem(USER_INFO)
  if (!loginInfo) {
    return null
  }
  return JSON.parse(loginInfo)
}

// 退出登陆时执行
export function clearLocalStorage() {
  window.localStorage.clear()
  window.sessionStorage.clear()
}
